package com.example.zjsr.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.zjsr.common.ServerResponse;
import com.example.zjsr.pojo.Commerce;
import com.example.zjsr.pojo.UserVo;
import com.example.zjsr.service.U3DRobotService;
import com.example.zjsr.util.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
@RequestMapping("/u3d/commerce")
@CrossOrigin
public class U3DRobotController {
    private static final Logger logger = LoggerFactory.getLogger(U3DRobotController.class);

    @Autowired
    private U3DRobotService u3DRobotService;

    /**
     * u3d插入数据到数据库中
     * @param commerce
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> addRobotData(@RequestBody Commerce commerce, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        logger.info("u3d正在往数据库插入数据"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        logger.info("获取到的实验数据是:{}",commerce);
        response.setHeader("Access-Control-Allow-Origin", "*");
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        if(jwt.length()==0 || StringUtils.isEmpty(jwt)){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DRobotService.addScore(jwt,commerce);
    }
}